package com.tianqu.tms.www.car.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code39Encoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WideRatioCodedPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang
 * 高级搜索打印准交证，检测报告处理
 */
public class CarSearchPrintAction extends TmsAction{
	private Logger logger = Logger.getLogger(CarSearchPrintAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String saveFlag = "";
		if(request.getParameter("saveFlag")!=null && !"".equals(request.getParameter("saveFlag")))
		{
			saveFlag = request.getParameter("saveFlag");		
		}
		
		CarCreationInputViewHelper viewhelper = new CarCreationInputViewHelper();
		viewhelper = (CarCreationInputViewHelper)uc.getAttribute(VIEWHELP);
		Codes code = Codes.getInstance();
		
		if("MT".equals(viewhelper.getGearbox())){
			viewhelper.setGearbox("手动");
		}
		if("AT".equals(viewhelper.getGearbox())){
			viewhelper.setGearbox("自动");
		}
		if("A/MT".equals(viewhelper.getGearbox())){
			viewhelper.setGearbox("手自一体");
		}
		if("0".equals(viewhelper.getDisplacement())){
			viewhelper.setDisplacementS("");
		}
		if("1".equals(saveFlag)){//打印准交证
			String onOff = "";
			onOff = Registry.getConfig(TmsConstants.WWW_IMAGE_ON_KEY);//1.开 2.关
			if("1".equals(onOff)){
				//未认证
				if(viewhelper.getAttestStatus() == 1){
					viewhelper.setImageOn("1");
				}else if(viewhelper.getAttestStatus() == 2){
					viewhelper.setImageOn("2");//认证
				}			
			}else{
				viewhelper.setImageOn("3");//无底板图片
			}
			
			try {  
	            JBarcode localJBarcode = new JBarcode(EAN13Encoder.getInstance(),WidthCodedPainter.getInstance(),EAN13TextPainter.getInstance());  
	            
	            String str = viewhelper.getTransferSerial()+viewhelper.getSerial(); 
	          
	            //BufferedImage localBufferedImage = localJBarcode.createBarcode(str);  
	            
	            //saveToJPEG(localBufferedImage, "EAN13.jpg");
	            localJBarcode.setEncoder(Code39Encoder.getInstance());   
	            localJBarcode.setPainter(WideRatioCodedPainter.getInstance());   
	            localJBarcode.setTextPainter(BaseLineTextPainter.getInstance());   
	            localJBarcode.setShowCheckDigit(false);   
	            //xx   
	            //str = "144500016";   
	            BufferedImage localBufferedImage = localJBarcode.createBarcode(str); 
	            String paramString = str+".jpg";           
	            viewhelper.setTiaoImage(Registry.getConfig(TmsConstants.WWW_UPLOAD_TIAO_UPLOAD_KEY)+"/"+paramString);
            	String path = request.getSession().getServletContext().getRealPath("/").replace("/tmsweb", "")+Registry.getConfig(TmsConstants.WWW_UPLOAD_TIAO_UPLOAD_KEY);
            	File file  = new File(path);
			    if (!file.exists()) {
			    	file.mkdirs();
				}
			   
            	FileOutputStream localFileOutputStream = new FileOutputStream(path+"/" + paramString);  
                ImageUtil.encodeAndWrite(localBufferedImage, "jpeg", localFileOutputStream, 96, 96);  
                localFileOutputStream.close();  
	            
	        }catch (Exception e) {  
	        	logger.error("createBarcode error",e);
	        }  
		}
		if("2".equals(saveFlag)){
			if(viewhelper.getSaveFlag() ==3){
				viewhelper.setRzFlag("on");//打印检测报告
			}
		}
		
		Member mem = Member.getInstance();
		viewhelper.setMemberName(viewhelper.getMemberId());

		viewhelper.setPrintFlag("1");
		request.setAttribute(VIEWHELP, viewhelper);
		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
